package org.fsdeduplicator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;

public class NavigationMenu {

    Action startAction, pauseAction, stopAction;
    JMenuBar menuBar;
    private FileDedupeFrame dedupeFrame;

    public NavigationMenu(FileDedupeFrame dedupeFrame) {
        this.dedupeFrame = dedupeFrame;

        startAction = new StartAction();
        pauseAction = new PauseAction();
        stopAction = new StopAction();

        startAction.setEnabled(false);
        pauseAction.setEnabled(false);
        stopAction.setEnabled(false);

        menuBar = new JMenuBar();
        
        menuBar.add(createFileMenu());
        menuBar.add(createDedupeMenu());
        menuBar.add(createHelpMenu());
    }

    private JMenu createFileMenu() {
        JMenu fileMenu = new JMenu("File");

        JMenuItem openItem = new JMenuItem("Set Root Directory...");
        openItem.setAccelerator(KeyStroke.getKeyStroke('O', KeyEvent.CTRL_MASK));
        openItem.addActionListener(new OpenListener());

        JMenuItem exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(new ExitListener());

        fileMenu.add(openItem);
        fileMenu.addSeparator();
        fileMenu.add(exitItem);

        return fileMenu;
    }

    private JMenu createDedupeMenu() {
        JMenu dedupeMenu = new JMenu("Dedupe");

        JMenuItem startItem = new JMenuItem(startAction);
        JMenuItem pauseItem = new JMenuItem(pauseAction);
        JMenuItem stopItem = new JMenuItem(stopAction);

        dedupeMenu.add(startItem);
        dedupeMenu.add(pauseItem);
        dedupeMenu.add(stopItem);

        return dedupeMenu;
    }

    private JMenu createHelpMenu() {
        JMenu helpMenu = new JMenu("Help");

        JMenuItem help = new JMenuItem("Help");
        help.setAccelerator(KeyStroke.getKeyStroke("F1"));
        help.addActionListener(new HelpListener());

        JMenuItem about = new JMenuItem("About");
        about.addActionListener(new AboutProgramListener());

        helpMenu.add(help);
        helpMenu.addSeparator();
        helpMenu.add(about);

        return helpMenu;
    }

    // 'File' items handlers

    private class OpenListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

            int result = fileChooser.showOpenDialog(dedupeFrame);
            if (result == JFileChooser.APPROVE_OPTION) {
                dedupeFrame.setRootLocation(fileChooser.getSelectedFile().getAbsolutePath());
            }
        }

    }

    private class ExitListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            dedupeFrame.doOnClose();
        }
    }

    // 'Dedupe' items handlers

    private class StartAction extends AbstractAction {
        public StartAction() {
            putValue(NAME, "Start");
            putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('S', KeyEvent.CTRL_MASK));
        }

        public void actionPerformed(ActionEvent e) {
            dedupeFrame.performStart();
        }
    }

    private class PauseAction extends AbstractAction {
        public PauseAction() {
            putValue(NAME, "Pause");
            putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('P', KeyEvent.CTRL_MASK));
        }

        public void actionPerformed(ActionEvent e) {
            dedupeFrame.performPause();
        }
    }

    private class StopAction extends AbstractAction {
        public StopAction() {
            putValue(NAME, "Stop");
            putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('T', KeyEvent.CTRL_MASK));
        }

        public void actionPerformed(ActionEvent e) {
            dedupeFrame.performStop();
        }
    }

    // 'Help' items handlers

    private class HelpListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {

        }
    }

    private class AboutProgramListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {

        }
    }
}
